<template>
	<view class="page-body">
		<!-- 页面内容开始 -->

		<!-- 表格搜索组件开始 -->
		<vk-data-table-query v-model="queryForm1.formData" :columns="queryForm1.columns"
			@search="search"></vk-data-table-query>
		<!-- 表格搜索组件结束 -->

		<!-- 自定义按钮区域开始 -->
		<view>
			<el-row>
				<el-button type="success" size="small" @click="exportExcel">导出表格数据</el-button>
			</el-row>
		</view>
		<!-- 自定义按钮区域结束 -->

		<!-- 提示 -->
		<view>
			<div class="notification">*每15分钟更新一次</div>
		</view>
		<el-radio-group v-model="activeName" size="medium" @change="typeChange">
			<el-radio-button label="account">账户</el-radio-button>
			<el-radio-button label="project">项目</el-radio-button>
			<el-radio-button label="ad">广告</el-radio-button>
		</el-radio-group>
		<!-- 表格组件开始 -->
		<vk-data-table v-if="activeName === 'account'" ref="table1" :action="table1.action" :columns="table1.columns" :query-form-param="queryForm1"
			:row-no="true" :pagination="true" stripe="true"  :border="true"
			@current-change="currentChange" key="account" :left-fixed="false"></vk-data-table>
		<vk-data-table v-else-if="activeName === 'project'" ref="table2" :action="table2.action" :columns="table2.columns" :query-form-param="queryForm1"
			:row-no="true" :pagination="true" stripe="true"  :border="true"
			@current-change="currentChange" key="project" :left-fixed="false"></vk-data-table>
		<vk-data-table v-else-if="activeName === 'ad'" ref="table3" :action="table3.action" :columns="table3.columns" :query-form-param="queryForm1"
			:row-no="true" :pagination="true" stripe="true"  :border="true"
			@current-change="currentChange" key="ad" :left-fixed="false"></vk-data-table>
		<!-- 表格组件结束 -->
		<!-- 页面内容结束 -->
	</view>
</template>

<script>
	var that; // 当前页面对象
	var vk = uni.vk; // vk实例
	var originalForms = {}; // 表单初始化数据
	// 表格字段显示规则
	let columns = [
		// {"key":"_id","title":"ID","type":"text","width":200},
		{
			"key": "stat_time_day",
			"title": "日期",
			"type": "text",
			"width": 200
		},
		{
			"key": "promotion_link_name",
			"title": "推广链接名称",
			"type": "html",
			"width": 200
		},
		
		// {
		// 	"key": "project_name",
		// 	"title": "项目名称",
		// 	"type": "text",
		// 	"width": 200
		// },
		// {
		// 	"key": "ad_name",
		// 	"title": "推广名称",
		// 	"type": "text",
		// 	"width": 200
		// },
		{
			"key": "promotion_link_id",
			"title": "推广链接ID",
			"type": "html",
			"width": 200
		},
		{
			"key": "mini_program_name",
			"title": "小程序名称",
			"type": "html",
			"width": 200
		},
		{
			"key": "platform",
			"title": "平台",
			"type": "html",
			"width": 200
		},
		{
			"key": "promotion_type",
			"title": "投放类型",
			"type": "html",
			"width": 200
		},
		{
			"key": "agency_name",
			"title": "机构名称",
			"type": "html",
			"width": 200
		},
		{
			"key": "short_drama_name",
			"title": "短剧名称",
			"type": "html",
			"width": 200
		},
		{
			"key": "account_expense",
			"title": "账面消耗",
			"type": "text",
			"width": 200
		},
		{
			"key": "ad_click_count",
			"title": "AD点击数",
			"type": "text",
			"width": 200
		},
		{
			"key": "ad_impressions_count",
			"title": "AD展现数量",
			"type": "text",
			"width": 200
		},
		{
			"key": "ad_conversion_count",
			"title": "AD转化数",
			"type": "text",
			"width": 200
		},
		{
			"key": "ad_conversion_cost",
			"title": "AD转化成本",
			"type": "text",
			"width": 200
		},
		{
			"key": "recharge_panel_name",
			"title": "充值面板名称",
			"type": "html",
			"width": 200
		},
		{
			"key": "reported_paid_count",
			"title": "回传付费数",
			"type": "text",
			"width": 200
		},
		{
			"key": "reported_paid_amount",
			"title": "回传付费金额",
			"type": "text",
			"width": 200
		},
		{
			"key": "total_paid_count",
			"title": "全量付费数",
			"type": "text",
			"width": 200
		},
		{
			"key": "total_paid_amount",
			"title": "全量付费金额",
			"type": "text",
			"width": 200
		},
		{
			"key": "total_paid_cost",
			"title": "付费成本（全量）",
			"type": "text",
			"width": 200
		},
		{
			"key": "total_roi",
			"title": "roi（全量）",
			"type": "text",
			"width": 200
		},
		{
			"key": "total_arppu",
			"title": "全量arppu",
			"type": "text",
			"width": 200
		},
		{
			"key": "actual_deduction_ratio",
			"title": "实际扣量比例",
			"type": "text",
			"width": 200
		}
	];
	let accountId = {
						"key": "account_id",
						"title": "账户ID",
						"type": "text",
						"width": 200
					};
	let projectId =	{
						"key": "project_id",
						"title": "项目ID",
						"type": "text",
						"width": 200
					};
    let projectName =	{
    					"key": "project_name",
    					"title": "项目名称",
    					"type": "text",
    					"width": 200
    				};
	let adId = {
					"key": "ad_id",
					"title": "广告ID",
					"type": "text",
					"width": 200
				};
	let adName = {
					"key": "ad_name",
					"title": "广告名称",
					"type": "text",
					"width": 200
				};
	let columns1 = [...columns];
	let columns2 = [...columns];
	let columns3 = [...columns];
	columns1.splice(2, 0, accountId);
	columns2.splice(2, 0, accountId, projectId, projectName);
	columns3.splice(2, 0, accountId, projectId, projectName, adId, adName);
	export default {
		data() {
			// 页面数据变量
			return {
				// 页面是否请求中或加载中
				loading: false,
				// init请求返回的数据
				data: {

				},
				activeName: 'account',
				// 表格相关开始 -----------------------------------------------------------
				table1: {
					// 表格数据请求地址
					action: (obj={})=>{
						let {
							data, // 请求数据
							success, // 成功回调
							fail, // 失败回调
							complete, // 成功回调
						} = obj;
						// 发起http请求
						vk.callFunction({
							name: 'leyouyinghua',
							url: `/api/kh/data/analyzer/getReportRealtimeAccount`,
							data: data,
							success: (res) => {
								if (typeof success === "function") {
									success({
										rows: res.rows, // 列表数据
										total: res.total, // 总记录数（如果没有总记录数，则需要返回getCount和hasMore）
										getCount: res.getCount, // 是否执行了count请求，当total的值不是总记录数时需要返回 getCount: false
										hasMore: res.hasMore, // 是否还有下一页，当total的值不是总记录数时需要返回是否还有下一页
									});
								}
							},
							fail: (res) => {
								if (typeof fail === "function") fail(res);
							},
							complete: (res) => {
								if (typeof complete === "function") complete(res);
							}
						});
					},
					columns: columns1,
					// 多选框选中的值
					multipleSelection: [],
					// 当前高亮的记录
					selectItem: ""
				},
				table2: {
					// 表格数据请求地址
					action: (obj={})=>{
						let {
							data, // 请求数据
							success, // 成功回调
							fail, // 失败回调
							complete, // 成功回调
						} = obj;
						// 发起http请求
						vk.callFunction({
							name: 'leyouyinghua',
							url: `/api/kh/data/analyzer/getReportRealtimeProject`,
							data: data,
							success: (res) => {
								if (typeof success === "function") {
									success({
										rows: res.rows, // 列表数据
										total: res.total, // 总记录数（如果没有总记录数，则需要返回getCount和hasMore）
										getCount: res.getCount, // 是否执行了count请求，当total的值不是总记录数时需要返回 getCount: false
										hasMore: res.hasMore, // 是否还有下一页，当total的值不是总记录数时需要返回是否还有下一页
									});
								}
							},
							fail: (res) => {
								if (typeof fail === "function") fail(res);
							},
							complete: (res) => {
								if (typeof complete === "function") complete(res);
							}
						});
					},
					columns: columns2,
				},
				table3: {
					// 表格数据请求地址
					action: (obj={})=>{
						let {
							data, // 请求数据
							success, // 成功回调
							fail, // 失败回调
							complete, // 成功回调
						} = obj;
						// 发起http请求
						vk.callFunction({
							name: 'leyouyinghua',
							url: `/api/kh/data/analyzer/getReportRealtimePromotion`,
							data: data,
							success: (res) => {
								if (typeof success === "function") {
									success({
										rows: res.rows, // 列表数据
										total: res.total, // 总记录数（如果没有总记录数，则需要返回getCount和hasMore）
										getCount: res.getCount, // 是否执行了count请求，当total的值不是总记录数时需要返回 getCount: false
										hasMore: res.hasMore, // 是否还有下一页，当total的值不是总记录数时需要返回是否还有下一页
									});
								}
							},
							fail: (res) => {
								if (typeof fail === "function") fail(res);
							},
							complete: (res) => {
								if (typeof complete === "function") complete(res);
							}
						});
					},
					columns: columns3,
				},
				// 表格相关结束 -----------------------------------------------------------
				// 表单相关开始 -----------------------------------------------------------
				// 查询表单请求数据
				queryForm1: {
					// 查询表单数据源，可在此设置默认值
					formData: {
						
					},
					// 查询表单的字段规则 fieldName:指定数据库字段名,不填默认等于key
					columns: [
						{ key: "agency_name", title: "机构名称", type: "text", width: 160, mode: "%%" },
						{ key: "account_id", title: "账户ID", type: "text", width: 140, mode: "%%" },
						{ key: "short_drama_name", title: "短剧名称", type: "text", width: 140, mode: "%%" },
						{
							key: "promotion_type",
							title: "投放类型",
							type: "select",
							width: 160,
							mode: "%%",
							data: [
								{ id: '0', name: '自投' },
								{ id: '1', name: '分销' },
								{ id: '2', name: '达人' },
							],
							props: { value: "name", label: "name" }
						},
						{
							"key": "promotion_link_id",
							"title": "推广链接ID",
							"type": "text",
							"width": 200,
							"mode": "%%"
						},
						{
							"key": "unix_timestamp",
							"title": "时间",
							"type": "datetimerange",
							"width": 400,
							"mode": "[]"
						}
					]
				},
				form1: {},
				// 其他弹窗表单
				formDatas: {},
				// 表单相关结束 -----------------------------------------------------------
			};
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			that = this;
			vk = that.vk;
			that.options = options;
			that.init(options);
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady() {},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {},
		// 函数
		methods: {
			typeChange() {
				let type = this.activeName === 'account' ? 'table1' : this.activeName === 'project' ? 'table2' : 'table3';
				that.$refs[type].search();
			},
			// 页面数据初始化函数
			init(options) {
				originalForms["form1"] = vk.pubfn.copyObject(that.form1);
			},
			// 页面跳转
			pageTo(path) {
				vk.navigateTo(path);
			},
			// 表单重置
			resetForm() {
				vk.pubfn.resetForm(originalForms, that);
			},
			// 搜索
			search() {
				let type = this.activeName === 'account' ? 'table1' : this.activeName === 'project' ? 'table2' : 'table3';
				that.$refs[type].search();
			},
			// 刷新
			refresh() {
				let type = this.activeName === 'account' ? 'table1' : this.activeName === 'project' ? 'table2' : 'table3';
				that.$refs[type].refresh();
			},
			// 获取当前选中的行的数据
			getCurrentRow() {
				let type = this.activeName === 'account' ? 'table1' : this.activeName === 'project' ? 'table2' : 'table3';
				return that.$refs[type].getCurrentRow();
			},
			// 监听 - 行的选中高亮事件
			currentChange(val) {
				let type = this.activeName === 'account' ? 'table1' : this.activeName === 'project' ? 'table2' : 'table3';
				that[type].selectItem = val;
			},
			// 当选择项发生变化时会触发该事件
			selectionChange(list) {
				let type = this.activeName === 'account' ? 'table1' : this.activeName === 'project' ? 'table2' : 'table3';
				that[type].multipleSelection = list;
			},
			// 到处表格数据
			exportExcel() {
				let type = this.activeName === 'account' ? 'table1' : this.activeName === 'project' ? 'table2' : 'table3';
				that.$refs[type].exportExcel({
				  fileName: "实时数据",
				  title: "正在导出数据...",
				  pageIndex: 1,
				  pageSize: -1, // 此值为-1，代表导出所有数据
				});
			},
			// 显示修改页面


		},
		// 监听属性
		watch: {

		},
		// 过滤器
		filters: {

		},
		// 计算属性
		computed: {

		}
	};
</script>
<style lang="scss" scoped>
	.page-body {}
	.notification {
		margin-top: 10px;
		color: grey;
		font-size: 12px;
	}
</style>